import type {
  OAuthConfigTemplate,
  ServiceProviderOAuthConfig,
  WechatOAuthConfig,
  AlipayOAuthConfig,
  CommonOAuthConfig
} from './oauth-config-types'

// 微信公众号配置模板
export const wechatMpTemplate: OAuthConfigTemplate = {
  platformType: 'WXPAY',
  configType: 'MP',
  fields: [
    {
      fieldKey: 'appId',
      fieldName: '公众号AppID',
      fieldType: 'input',
      required: true,
      placeholder: '请输入微信公众号AppID',
      description: '微信公众号的应用ID'
    },
    {
      fieldKey: 'appSecret',
      fieldName: '公众号AppSecret',
      fieldType: 'password',
      required: true,
      placeholder: '请输入微信公众号AppSecret',
      description: '微信公众号的应用密钥'
    },
    {
      fieldKey: 'token',
      fieldName: '消息校验Token',
      fieldType: 'input',
      required: false,
      placeholder: '请输入消息校验Token',
      description: '用于验证消息来源的Token'
    },
    {
      fieldKey: 'encodingAesKey',
      fieldName: '消息加解密密钥',
      fieldType: 'password',
      required: false,
      placeholder: '请输入消息加解密密钥',
      description: '用于消息加解密的AES密钥'
    },

  ]
}

// 微信小程序配置模板
export const wechatMiniappTemplate: OAuthConfigTemplate = {
  platformType: 'WXPAY',
  configType: 'MINIAPP',
  fields: [
    {
      fieldKey: 'appId',
      fieldName: '小程序AppID',
      fieldType: 'input',
      required: true,
      placeholder: '请输入微信小程序AppID',
      description: '微信小程序的应用ID'
    },
    {
      fieldKey: 'appSecret',
      fieldName: '小程序AppSecret',
      fieldType: 'password',
      required: true,
      placeholder: '请输入微信小程序AppSecret',
      description: '微信小程序的应用密钥'
    },
    {
      fieldKey: 'token',
      fieldName: '消息校验Token',
      fieldType: 'input',
      required: false,
      placeholder: '请输入消息校验Token',
      description: '用于验证消息来源的Token'
    },
    {
      fieldKey: 'encodingAesKey',
      fieldName: '消息加解密密钥',
      fieldType: 'password',
      required: false,
      placeholder: '请输入消息加解密密钥',
      description: '用于消息加解密的AES密钥'
    },

  ]
}

// 微信开放平台配置模板
export const wechatOpenTemplate: OAuthConfigTemplate = {
  platformType: 'WXPAY',
  configType: 'OPEN',
  fields: [
    {
      fieldKey: 'appId',
      fieldName: '开放平台AppID',
      fieldType: 'input',
      required: true,
      placeholder: '请输入微信开放平台AppID',
      description: '微信开放平台的应用ID'
    },
    {
      fieldKey: 'appSecret',
      fieldName: '开放平台AppSecret',
      fieldType: 'password',
      required: true,
      placeholder: '请输入微信开放平台AppSecret',
      description: '微信开放平台的应用密钥'
    },

  ]
}

// 支付宝应用配置模板
export const alipayAppTemplate: OAuthConfigTemplate = {
  platformType: 'ALIPAY',
  configType: 'APP',
  fields: [
    {
      fieldKey: 'appId',
      fieldName: '支付宝应用ID',
      fieldType: 'input',
      required: true,
      placeholder: '请输入支付宝应用ID',
      description: '支付宝开放平台的应用ID'
    },
    {
      fieldKey: 'privateKey',
      fieldName: '应用私钥',
      fieldType: 'textarea',
      required: true,
      placeholder: '请输入应用私钥',
      description: '支付宝应用的RSA私钥'
    },
    {
      fieldKey: 'alipayPublicKey',
      fieldName: '支付宝公钥',
      fieldType: 'textarea',
      required: true,
      placeholder: '请输入支付宝公钥',
      description: '支付宝平台的RSA公钥'
    },

  ]
}

// 支付宝OAuth配置模板
export const alipayOAuthTemplate: OAuthConfigTemplate = {
  platformType: 'ALIPAY',
  configType: 'OAUTH',
  fields: [
    {
      fieldKey: 'scope',
      fieldName: '授权范围',
      fieldType: 'select',
      required: true,
      description: '支付宝OAuth授权范围',
      options: [
        { label: '基础信息授权', value: 'auth_base' },
        { label: '用户信息授权', value: 'auth_user' },
        { label: '用户信息授权,基础信息授权', value: 'auth_user,auth_base' }
      ],
      defaultValue: ['auth_user']
    },
    {
      fieldKey: 'redirectUri',
      fieldName: '回调地址',
      fieldType: 'input',
      required: true,
      placeholder: '请输入OAuth回调地址',
      description: '用户授权后的回调地址'
    },

  ]
}

// 通用配置模板
export const commonConfigTemplate = {
  fields: [
    {
      fieldKey: 'environment',
      fieldName: '环境配置',
      fieldType: 'select',
      required: true,
      description: '选择运行环境',
      options: [
        { label: '生产环境', value: 'PRODUCTION' },
        { label: '沙箱环境', value: 'SANDBOX' }
      ],
      defaultValue: 'SANDBOX'
    },
    {
      fieldKey: 'callbackDomain',
      fieldName: '回调域名',
      fieldType: 'input',
      required: true,
      placeholder: '请输入回调域名',
      description: '第三方授权回调的域名'
    },
    {
      fieldKey: 'encryptionKey',
      fieldName: '加密密钥',
      fieldType: 'password',
      required: false,
      placeholder: '请输入加密密钥',
      description: '用于敏感数据加密的密钥'
    }
  ]
}

// 创建默认配置数据
export function createDefaultWechatOAuthConfig(): WechatOAuthConfig {
  return {
    mp: {
      appId: '',
      appSecret: '',
      token: '',
      encodingAesKey: ''
    },
    miniapp: {
      appId: '',
      appSecret: '',
      token: '',
      encodingAesKey: ''
    },
    open: {
      appId: '',
      appSecret: ''
    }
  }
}

export function createDefaultAlipayOAuthConfig(): AlipayOAuthConfig {
  return {
    app: {
      appId: '',
      privateKey: '',
      alipayPublicKey: ''
    },
    oauth: {
      scope: ['auth_user'],
      redirectUri: ''
    }
  }
}

export function createDefaultCommonConfig(): CommonOAuthConfig {
  return {
    environment: 'SANDBOX',
    callbackDomain: '',
    encryptionKey: ''
  }
}

export function createDefaultOAuthConfig(providerId: string, providerName: string): ServiceProviderOAuthConfig {
  return {
    id: '',
    providerId,
    providerName,
    wechatOAuth: createDefaultWechatOAuthConfig(),
    alipayOAuth: createDefaultAlipayOAuthConfig(),
    commonConfig: createDefaultCommonConfig(),
    status: 'ACTIVE',
    createdTime: new Date().toLocaleString(),
    updatedTime: new Date().toLocaleString()
  }
}

// 导出所有模板
export const oauthConfigTemplates = {
  wechatMp: wechatMpTemplate,
  wechatMiniapp: wechatMiniappTemplate,
  wechatOpen: wechatOpenTemplate,
  alipayApp: alipayAppTemplate,
  alipayOAuth: alipayOAuthTemplate,
  common: commonConfigTemplate
}